package cn.zly.spring.core.io;

import cn.hutool.core.util.ClassUtil;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

/**
 * @author zhanglianyong
 * @date 2025/1/2
 */
public class ClassPathResource implements Resource {

	private String path;

	private ClassLoader classLoader;

	public ClassPathResource(String path) {
		this(path, null);
	}

	public ClassPathResource(String path, ClassLoader classLoader) {
		this.path = path;
		this.classLoader = classLoader != null ? classLoader : ClassUtil.getClassLoader();
	}

	@Override
	public InputStream getInputStream() throws IOException {
		InputStream inputStream = classLoader.getResourceAsStream(path);
		if (inputStream == null) {
			throw new FileNotFoundException("file" + path + "not found");
		}
		return inputStream;
	}
}
